package com.cash.channel.web.controller;

import com.cash.channel.web.annotation.RestControllerMapper;
import com.cash.channel.web.annotation.ServiceLog;
import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.api.util.ValidatorUtil;
import com.cash.channel.web.model.RequestModel;
import com.cash.channel.api.util.ApiUtils;
import com.cash.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@Slf4j
@RestControllerMapper("/bank")
public class BankController extends BaseController {

    @PostMapping("/card/list")
    @ServiceLog("获取银行卡列表")
    public Result cardList(@RequestBody RequestModel requestModel) {
        try {
            return doDispatch("bankCardList", requestModel, dataMap -> {
                ApiUtils.checkParamsExist(dataMap, "userId");});
        } catch (CashChannelException e) {
            log.error(e.getErrMessage(), e);
            return Result.failed(e.getErrMessage());
        }
    }

    @PostMapping("/card/bind")
    public Result cardBind(@RequestBody RequestModel requestModel) {
        try {
            return doDispatch("bankCardBind", requestModel, dataMap -> {
                ApiUtils.checkParamsExist(dataMap, "busiType","orderNo","idNo","name","cardNo");
                if (!ValidatorUtil.checkIdNo(MapUtils.getString(dataMap, "idNo"))) {
                    throw new CashChannelException("身份证["+MapUtils.getString(dataMap, "idNo")+"]格式错误");
                }
                if (1 == MapUtils.getInteger(dataMap, "busiType")) {
                    ApiUtils.checkParamsExist(dataMap, "bankCode","bankName","reserveMobile");
                    if (!ValidatorUtil.isMobile(MapUtils.getString(dataMap, "reserveMobile"))) {
                        throw new CashChannelException("手机号["+MapUtils.getString(dataMap, "reserveMobile")+"]格式错误");
                    }
                }
            });
        } catch (CashChannelException e) {
            log.error(e.getErrMessage(), e);
            return Result.failed(e.getErrMessage());
        }
    }

    @PostMapping("/list")
    @ServiceLog("获取支持银行列表")
    public Result list(@RequestBody RequestModel requestModel) {
        try {
            return doDispatch("bankList" ,requestModel, dataMap -> {
                ApiUtils.checkParamsExist(dataMap, "userId");
            });
        } catch (CashChannelException e) {
            log.error(e.getErrMessage(), e);
            return Result.failed(e.getErrMessage());
        }
    }

    @PostMapping("/card/bind/captcha")
    public Result cardBindCaptcha(@RequestBody RequestModel requestModel) {
        try {
            return doDispatch("bankCardBindCaptcha", requestModel, dataMap -> {
                ApiUtils.checkParamsExist(dataMap, "verifyCode","orderNo","idNo","name","cardNo","bankCode","reserveMobile");
                if (!ValidatorUtil.checkIdNo(MapUtils.getString(dataMap, "idNo"))) {
                    throw new CashChannelException("身份证["+MapUtils.getString(dataMap, "idNo")+"]格式错误");
                }
                if (!ValidatorUtil.isMobile(MapUtils.getString(dataMap, "reserveMobile"))) {
                    throw new CashChannelException("手机号["+MapUtils.getString(dataMap, "reserveMobile")+"]格式错误");
                }
            });
        } catch (CashChannelException e) {
            log.error(e.getErrMessage(), e);
            return Result.failed(e.getErrMessage());
        }
    }

}
